package com.abe.bathsystem.base;

import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.SPUtils;

public class ManagerTimer {
    private static final String SP_NAME = "com.abe.bath";
    private static final String SP_AD_TIME = "SP_AD_TIME";
    private static final String SP_LOCK_TIME = "SP_LOCK_TIME";

    //毫秒(无操作落锁时间)
    private static final int SHOW_AD_OUT_TIME = 20 * 1000;//默认20秒
    //毫秒(无操作显示广告时间)
    private static final int UPDATE_AD_TIME = 60 * 60 * 1000;//默认1小时
    //图片播放默认时间
    public static final int IMAGE_PLAY_TIME = 5;//默认5秒
    //传感器上报默认时间
    public static final int SENSOR_UP_TIME = 5;//默认5秒
    //断电时间
    public static final int SURVIVAL_INTERVAL=60;//存活时间间隔超过60秒，即视为断电


    public static int adUpdateTime() {
        SPUtils spUtils = new SPUtils(SP_NAME);
        return spUtils.getInt(SP_AD_TIME, UPDATE_AD_TIME);
    }

    public static void setAdUpdateTime(int time) {
        if (time <= 0) return;
        SPUtils spUtils = new SPUtils(SP_NAME);
        spUtils.putInt(SP_AD_TIME, time);
    }

    public static int autoLockTime() {
        SPUtils spUtils = new SPUtils(SP_NAME);
        return spUtils.getInt(SP_LOCK_TIME, SHOW_AD_OUT_TIME);
    }

    public static void setAutoLockTime(int time) {
        if (time <= 0) return;
        SPUtils spUtils = new SPUtils(SP_NAME);
        spUtils.putInt(SP_LOCK_TIME, time);
    }

    //时间（毫秒）转化为时分秒格式
    public static String format(int time) {
        int totalSeconds = time / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        StringBuffer timeStr = new StringBuffer();
        if (hours > 0) timeStr.append(hours).append("小时");
        if (minutes > 0) timeStr.append(minutes).append("分钟");
        if (seconds > 0) timeStr.append(seconds).append("秒");
        return timeStr.toString();
    }

    //时分秒转化为时间（毫秒）
    public static int parse(String time) {
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        int x1 = time.indexOf("小时");
        if (x1 != -1) {
            CommonUtils.w("小时x:" + x1);
            String hoursStr = time.substring(0, x1);
            CommonUtils.w("hoursStr:" + hoursStr);
            hours = Integer.parseInt(hoursStr);
        }
        int x2 = time.indexOf("分钟");
        if (x2 != -1) {
            CommonUtils.w("分钟x:" + x2);
            int start = x1 != -1 ? x1 + 2 : 0;
            String minutesStr = time.substring(start, x2);
            CommonUtils.w("minutesStr:" + minutesStr);
            minutes = Integer.parseInt(minutesStr);
        }
        int x3 = time.indexOf("秒");
        if (x3 != -1) {
            CommonUtils.w("秒x:" + x3);
            int start = 0;
            if (x2 != -1) {
                start = x2 + 2;
            } else {
                start = x1 != -1 ? x1 + 2 : 0;
            }
            String secondsStr = time.substring(start, x3);
            CommonUtils.w("secondsStr:" + secondsStr);
            seconds = Integer.parseInt(secondsStr);
        }
        int totalSeconds = hours * 3600 + minutes * 60 + seconds;
        return totalSeconds * 1000;
    }
}